package ru.ssau.berlin.jsf.converters;

import java.util.logging.*;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.*;
import ru.ssau.berlin.ejb.facade.*;
import ru.ssau.berlin.entity.AttributeType;
import ru.ssau.berlin.framework.JNDIHelper;


public class AttributeTypeConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        InitialContext ic;
        AttributeType type = null;
        try {
            ic = new InitialContext();
            UniFacadeLocal facade =
                    (UniFacadeLocal) ic.lookup(JNDIHelper.getBeanName(UniFacade.class));
            type = facade.findAttributeType(Long.parseLong(value));
        } catch (NamingException ex) {
            Logger.getLogger(PersonTypeConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return type;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((AttributeType) value).getId().toString();
    }

}
